﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADC.DAL;

namespace ADC.BL
{

    public class ProductFactory
    {
        /// <summary>
        /// Get all programes
        /// </summary>
        /// <returns></returns>
        public static List<Product> GetAll(int typeID)
        {
            var db = new dbADCEntities();
            List<Product> listPrograme = db.Products.Where(g => g.ProductType == typeID).ToList(); 
            return listPrograme;
        }

        public static List<Product> GetAllHot()
        {
            var db = new dbADCEntities();
            List<Product> listPrograme = db.Products.Where(g => g.IsHot == true).ToList();
            return listPrograme;
        }

        public static List<Product> GetHotProduct(int numberHot)
        {
            var db = new dbADCEntities();
            List<Product> listPrograme = db.Products.Where(g => g.IsHot == true).OrderByDescending(x => x.ModifiedDate).Take(numberHot).ToList();
            return listPrograme;
        }

        /// <summary>
        /// Get progrme by ProgrameID
        /// </summary>
        /// <param name="programeID"></param>
        /// <returns></returns>
        public static Product GetByID(int productID) 
        {
            var db = new dbADCEntities();
            return db.Products.Where(g => g.ProductID == productID).FirstOrDefault();
        }

        /// <summary>
        /// Update Programe
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public static int Update(Product product)
        {
            try
            {
                var db = new dbADCEntities();
                var objProduct = new Product();
                objProduct = db.Products.Where(g => g.ProductID == product.ProductID).FirstOrDefault();


                objProduct.Name = product.Name;
                objProduct.Description = product.Description;
                objProduct.Model = product.Model;
                objProduct.MadeIn = product.MadeIn;
                objProduct.Price = product.Price;
                objProduct.Tech = product.Tech;
                objProduct.Warranty = product.Warranty;
                objProduct.ImagePath = product.ImagePath;
                objProduct.ProductType = product.ProductType;
                objProduct.IsHot = product.IsHot;
                objProduct.ModifiedBy = product.ModifiedBy;
                objProduct.ModifiedDate = DateTime.Now;
                return db.SaveChanges();
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
        }

        /// <summary>
        /// Get List programe by Type
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static List<Product> GetByType(int type)
        {
            var db = new dbADCEntities();
            List<Product> programs = db.Products.Where(g => g.ProductID == type).ToList();            
            return programs;
        }

        //public static List<Program> GetProgramByType(int type)
        //{
        //    var db = new dbADCEntities();
        //    List<Program> programs = db.Programs.Where(g => g.Type == type).ToList();
        //    return programs;
        //}

        public static Product Insert(Product product)
        {
            var db = new dbADCEntities();
            db.AddToProducts(product);
            db.SaveChanges();
            return product;
        }

        public static int Delete(int productID) 
        {
            var db = new dbADCEntities();
            // Delete product
            var program = db.Products.Where(g => g.ProductID.Equals(productID)).FirstOrDefault();
            db.DeleteObject(program);
            return db.SaveChanges();
        }


        /// <summary>
        /// Counts the program that belong of programType.
        /// </summary>
        /// <param name="typeID">The type ID.</param>
        /// <returns></returns>
        public static int CountProgram(int productTypeID)
        {
            try
            {
                var db = new dbADCEntities();
                var objProgram = db.Products.Where(g => g.ProductType == productTypeID).ToList();
                return objProgram.Count;
            }
            catch (Exception)
            {
                return -1;
                // Log here
                throw;
            }
        }
    }
}
